<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
            <el-form-item label="门店编号" prop="store_code">
                <el-input :disabled="true" v-model="form.store_code" clearable></el-input>
            </el-form-item>
            <el-form-item label="门店名称" prop="store_name">
                <el-input :disabled="true" v-model="form.store_name" clearable></el-input>
            </el-form-item>
            <el-form-item label="医生编号" prop="doctor_code">
                <el-input :disabled="true" v-model="form.doctor_code" clearable></el-input>
            </el-form-item>
            <el-form-item label="医生名字" prop="doctor_name">
                <el-input :disabled="true" v-model="form.doctor_name" clearable></el-input>
            </el-form-item>
            <el-form-item label="医生简介" prop="doctor_intro">
                <el-input type="textarea" :autosize="{minRows: 4}" v-model="form.doctor_intro" clearable></el-input>
            </el-form-item>
            <el-form-item label="是否有效" prop="status">
                <el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false" >取 消</el-button>
            <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
    export default {
        emits: ['success', 'closed'],
        data() {
            return {
                mode: "add",
                titleMap: {
                    add: '新增医生信息',
                    edit: '编辑医生信息'
                },
                form: {
                    id:"",
                    store_name: "",
                    store_code: "",
                    status: 1,
                    doctor_code:"",
                    doctor_name:"",
                    doctor_intro:""
                },
                rules: {
                    doctor_intro:[
                        {required: true, message: '医生简介必填'}
                    ]
                },
                visible: false,
                isSaveing: false
            }
        },
        methods: {
            //显示
            open(mode='add'){
                this.mode = mode;
                this.visible = true;
                return this;
            },
            //表单提交方法
            submit(){
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res = [];
                        if(this.mode=='add'){
                            res = await this.$API.bole.doctor.create.post(this.form);
                        }else{
                            res = await this.$API.bole.doctor.update.post(this.form);
                        }
                        this.isSaveing = false;
                        if(res.code == 0){
                            this.$emit('success', this.form, this.mode)
                            this.visible = false;
                            this.$message.success("操作成功")
                        }else{
                            this.$alert(res.message, "提示", {type: 'error'})
                        }
                    }
                })
            },
            //表单注入数据
            setData(data){
                Object.keys(this.form).forEach(key=>{
                    this.form[key] = data[key];
                })
            }
        }
    }
</script>

<style>
</style>
